﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 本周均值
    /// </summary>
    public class OperatorAvgWeek
    {
        public int Id { get; set; }
        /// <summary>
        /// 用户编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }

        public int Month { get; set; }

        public int Week { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }
        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }
        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }
        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }
        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }
        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }



        ///// <summary>
        ///// 初始化交易员周均值
        ///// </summary>
        ///// <param name="operatorId"></param>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="db"></param>
        ///// <returns></returns>
        //public void InitOperatorAvgWeek(int tradeHistoryId, DB.DBManager db)
        //{
        //    Sys.TradeHistory th = new Sys.TradeHistory();
        //    th = th.GetModel(tradeHistoryId, db);
        //    Sys.DayOperatorAccount doa = new Sys.DayOperatorAccount();

        //    #region 得到本周所有的交易日

        //    List<Sys.TradeHistory> tradeDays = th.GetWeekTradeingDay(th.WPdate, th.YPdate, db);

        //    #endregion

        //    var query = new Sys.Operator().GetList(db);


        //    foreach (Sys.Operator op in query)
        //    {
        //        List<Data.Day.AccountDayChange> change = new List<Data.Day.AccountDayChange>();
        //        List<Data.Day.AccountDayAnalysis> analysis = new List<Data.Day.AccountDayAnalysis>();

        //        #region 得到每个交易日用户的账户交易情况
        //        foreach (Sys.TradeHistory h in tradeDays)
        //        {
        //            List<Sys.Account> accounts = doa.GetDayOperatorAccounts(h.GetDateTimeFromTradeHistoryId(h.Id, db), op.Id, db);
        //            foreach (Sys.Account account in accounts)
        //            {
        //                Data.Day.AccountDayChange adc = new Data.Day.AccountDayChange();
        //                adc = adc.GetAccountDayChange(h.Id, account.Id, db);
        //                change.Add(adc);

        //                Data.Day.AccountDayAnalysis ada = new Data.Day.AccountDayAnalysis();
        //                ada = ada.GetAccountDayAnalysis(h.Id, account.Id, db);
        //                analysis.Add(ada);
        //            }
        //            View.OperatorAvgWeek oaw = new OperatorAvgWeek();
        //            var exist = db.OperatorAvgWeek.Where(a => a.Week == th.WPdate).Where(a => a.Year == th.YPdate).ToList();
        //            if (exist.Count == 0)
        //            {
        //                oaw.Year = th.YPdate;
        //                oaw.Month = th.MPdate;
        //                oaw.Week = th.WPdate;

        //                if (change.Count > 0)
        //                {
        //                    oaw.AvgBillBenefit = Math.Round(analysis.Sum(a => a.AvgBillBenefit) / change.Count(), 2);
        //                    oaw.AvgPostionTime = Math.Round(analysis.Sum(a => a.AvgPostionTime) / change.Count(), 2);
        //                    oaw.BillCount = Math.Round(change.Sum(a => a.BillCount) / change.Count(), 2);
        //                    oaw.DayCount = Math.Round(change.Sum(a => a.DayCount) / change.Count(), 2);
        //                    oaw.Free = Math.Round(change.Sum(a => a.Free) / change.Count(), 2);
        //                    oaw.LoseCount = Convert.ToInt32(change.Sum(a => a.LoseCount) / change.Count());
        //                    oaw.MaxReturn = Math.Round(analysis.Sum(a => a.MaxReturn) / change.Count(), 2);
        //                    oaw.OperatorId = op.Id;
        //                    oaw.OverStopLoss = Math.Round(analysis.Sum(a => a.OverStopLoss) / change.Count(), 2);
        //                    oaw.PingJinPing = Convert.ToInt32(analysis.Sum(a => a.PingJinPing) / change.Count());
        //                    oaw.Profit = Math.Round(change.Sum(a => a.Profit) / change.Count(), 2);
        //                    oaw.RaiseRate = Math.Round(analysis.Sum(a => a.RaiseRate) / change.Count(), 2);
        //                    oaw.RiskCount = Convert.ToInt32(analysis.Sum(a => a.RiskCount) / change.Count());
        //                    oaw.SumLost = Math.Round(change.Sum(a => a.SumLost) / change.Count(), 2);
        //                    oaw.SumWin = Math.Round(change.Sum(a => a.SumWin) / change.Count(), 2);

        //                    oaw.UsedMargin = Math.Round(change.Sum(a => a.UsedMargin) / change.Count(), 2);
        //                    oaw.Utilization = Math.Round(analysis.Sum(a => a.Utilization) / change.Count(), 2);
        //                    oaw.WinCount = Convert.ToInt32(change.Sum(a => a.WinCount) / change.Count());
        //                    oaw.WinRate = Math.Round(analysis.Sum(a => a.WinRate) / change.Count(), 2);
        //                    oaw.Yield = Math.Round(analysis.Sum(a => a.Yield) / change.Count(), 2);
        //                    db.OperatorAvgWeek.Add(oaw);
        //                }
        //            }
        //            else
        //            {
        //                if (change.Count > 0)
        //                {
        //                    exist.First().AvgBillBenefit = Math.Round(analysis.Sum(a => a.AvgBillBenefit) / change.Count(), 2);
        //                    exist.First().AvgPostionTime = Math.Round(analysis.Sum(a => a.AvgPostionTime) / change.Count(), 2);
        //                    exist.First().BillCount = Math.Round(change.Sum(a => a.BillCount) / change.Count(), 2);
        //                    exist.First().DayCount = Math.Round(change.Sum(a => a.DayCount) / change.Count(), 2);
        //                    exist.First().Free = Math.Round(change.Sum(a => a.Free) / change.Count(), 2);
        //                    exist.First().LoseCount = Convert.ToInt32(change.Sum(a => a.LoseCount) / change.Count());
        //                    exist.First().MaxReturn = Math.Round(analysis.Sum(a => a.MaxReturn) / change.Count(), 2);
        //                    exist.First().OperatorId = op.Id;
        //                    exist.First().OverStopLoss = Math.Round(analysis.Sum(a => a.OverStopLoss) / change.Count(), 2);
        //                    exist.First().PingJinPing = Convert.ToInt32(analysis.Sum(a => a.PingJinPing) / change.Count());
        //                    exist.First().Profit = Math.Round(change.Sum(a => a.Profit) / change.Count(), 2);
        //                    exist.First().RaiseRate = Math.Round(analysis.Sum(a => a.RaiseRate) / change.Count(), 2);
        //                    exist.First().RiskCount = Convert.ToInt32(analysis.Sum(a => a.RiskCount) / change.Count());
        //                    exist.First().SumLost = Math.Round(change.Sum(a => a.SumLost) / change.Count(), 2);
        //                    exist.First().SumWin = Math.Round(change.Sum(a => a.SumWin) / change.Count(), 2);

        //                    exist.First().UsedMargin = Math.Round(change.Sum(a => a.UsedMargin) / change.Count(), 2);
        //                    exist.First().Utilization = Math.Round(analysis.Sum(a => a.Utilization) / change.Count(), 2);
        //                    exist.First().WinCount = Convert.ToInt32(change.Sum(a => a.WinCount) / change.Count());
        //                    exist.First().WinRate = Math.Round(analysis.Sum(a => a.WinRate) / change.Count(), 2);
        //                    exist.First().Yield = Math.Round(analysis.Sum(a => a.Yield) / change.Count(), 2);
        //                }
        //            }
        //            #region 取均值


        //            #endregion
        //        }
        //        #endregion


        //    }
        //    db.SaveChanges();
        //}


        ///// <summary>
        ///// 获取交易员的周均值
        ///// </summary>
        ///// <param name="operatorId"></param>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="db"></param>
        ///// <returns></returns>
        //public OperatorAvgWeek GetOperatorAvgWeek(int operatorId, int year, int week, DB.DBManager db)
        //{
        //    var query = db.OperatorAvgWeek.Where(a => a.OperatorId == operatorId).Where(a => a.Year == year).Where(a => a.Week == week);
        //    if (query.Count() > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return new OperatorAvgWeek();
        //    }
        //}


        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var th = db.TradeHistorys.Find(tradeHistoryId);
        //    var query = db.OperatorAvgWeek.Where(a => a.Year == th.YPdate).Where(a => a.Week == th.WPdate);
        //    foreach (OperatorAvgWeek w in query)
        //    {
        //        db.OperatorAvgWeek.Remove(w);
        //    }
        //    db.SaveChanges();
        //}
    }
}
